{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[[0.2000, 0.3000],\n",
       "          [0.4000, 0.5000]],\n",
       "\n",
       "         [[0.1000, 0.6000],\n",
       "          [0.1000, 0.6000]],\n",
       "\n",
       "         [[0.1000, 0.7000],\n",
       "          [0.1000, 0.5000]]]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = torch.tensor([\n",
    "    [\n",
    "        # channel 0\n",
    "        [0.2, 0.3],\n",
    "        [0.4, 0.5]\n",
    "    ],\n",
    "    [\n",
    "        # channel 1\n",
    "        [0.1, 0.6],\n",
    "        [0.1, 0.6]\n",
    "    ],\n",
    "    [\n",
    "        # channel 2\n",
    "        [0.1, 0.7],\n",
    "        [0.1, 0.5]\n",
    "    ],\n",
    "])\n",
    "t = t.unsqueeze(0)\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([1, 1, 2, 2]) torch.Size([1, 1, 2, 2])\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "torch.return_types.max(\n",
       "values=tensor([[[[0.2000, 0.7000],\n",
       "          [0.4000, 0.6000]]]]),\n",
       "indices=tensor([[[[0, 2],\n",
       "          [0, 1]]]]))"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m = torch.max(t, dim=1, keepdim=True)\n",
    "print(m[0].size(), m[1].size())\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([1, 3, 2, 2])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nonmax = (t < m[0])\n",
    "nonmax\n",
    "t[nonmax] = 0\n",
    "t.size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0.2000, 0.7000],\n",
       "         [0.4000, 0.6000]],\n",
       "\n",
       "        [[0.2000, 0.7000],\n",
       "         [0.4000, 0.6000]],\n",
       "\n",
       "        [[0.2000, 0.7000],\n",
       "         [0.4000, 0.6000]]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = torch.zeros_like(t)\n",
    "z[m[1]] = m[0]\n",
    "z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
